package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Asignatura;


public class AsignaturaDAO implements IAsignaturaDAO {
    private static final Log log = LogFactory.getLog(AsignaturaDAO.class);

    // property constants
    //public static final String  ASIGDESCRIPV = "asigdescripv";
    public static final String ASIGDESCRIPV = "asigdescripv";

    //public static final Long  ASIGDESVNACIONAL = "asigdesvnacional";
    public static final String ASIGDESVNACIONAL = "asigdesvnacional";

    //public static final Long  ASIGDESVNACSABER = "asigdesvnacsaber";
    public static final String ASIGDESVNACSABER = "asigdesvnacsaber";

    //public static final Integer  ASIGIDN = "asigidn";
    public static final String ASIGIDN = "asigidn";

    //public static final Long  ASIGPROMNACIONAL = "asigpromnacional";
    public static final String ASIGPROMNACIONAL = "asigpromnacional";

    //public static final Long  ASIGPROMNACSABER = "asigpromnacsaber";
    public static final String ASIGPROMNACSABER = "asigpromnacsaber";

    //public static final Integer  ASIGXEXAMORDFN = "asigxexamordfn";
    public static final String ASIGXEXAMORDFN = "asigxexamordfn";

    //public static final Integer  ASIGXEXAMORDN2N = "asigxexamordn2n";
    public static final String ASIGXEXAMORDN2N = "asigxexamordn2n";

    //public static final Integer  ASIGXEXAMORDNN = "asigxexamordnn";
    public static final String ASIGXEXAMORDNN = "asigxexamordnn";

    //public static final Integer  ASIGXEXAMORDSN = "asigxexamordsn";
    public static final String ASIGXEXAMORDSN = "asigxexamordsn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Asignatura Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Asignatura instance) {
        log.debug("saving Asignatura instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Asignatura Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Asignatura instance) {
        log.debug("deleting Asignatura instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Asignatura Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Asignatura instance) {
        log.debug("updating Asignatura instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Asignatura findById(Integer id) {
        log.debug("finding Asignatura instance with id: " + id);

        try {
            Asignatura instance = (Asignatura) getSession()
                                                   .get(Asignatura.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Asignatura failed", re);
            throw re;
        }
    }

    public List<Asignatura> findByExample(Asignatura instance) {
        log.debug("finding Asignatura instance by example");

        try {
            List results = getSession().createCriteria("Asignatura")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Asignatura entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Asignatura> found by query
        */
    public List<Asignatura> findByProperty(String propertyName, Object value) {
        log.debug("finding Asignatura instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Asignatura as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Asignatura> findByAsigdescripv(Object asigdescripv) {
        return findByProperty(ASIGDESCRIPV, asigdescripv);
    }

    public List<Asignatura> findByAsigdesvnacional(Object asigdesvnacional) {
        return findByProperty(ASIGDESVNACIONAL, asigdesvnacional);
    }

    public List<Asignatura> findByAsigdesvnacsaber(Object asigdesvnacsaber) {
        return findByProperty(ASIGDESVNACSABER, asigdesvnacsaber);
    }

    public List<Asignatura> findByAsigidn(Object asigidn) {
        return findByProperty(ASIGIDN, asigidn);
    }

    public List<Asignatura> findByAsigpromnacional(Object asigpromnacional) {
        return findByProperty(ASIGPROMNACIONAL, asigpromnacional);
    }

    public List<Asignatura> findByAsigpromnacsaber(Object asigpromnacsaber) {
        return findByProperty(ASIGPROMNACSABER, asigpromnacsaber);
    }

    public List<Asignatura> findByAsigxexamordfn(Object asigxexamordfn) {
        return findByProperty(ASIGXEXAMORDFN, asigxexamordfn);
    }

    public List<Asignatura> findByAsigxexamordn2n(Object asigxexamordn2n) {
        return findByProperty(ASIGXEXAMORDN2N, asigxexamordn2n);
    }

    public List<Asignatura> findByAsigxexamordnn(Object asigxexamordnn) {
        return findByProperty(ASIGXEXAMORDNN, asigxexamordnn);
    }

    public List<Asignatura> findByAsigxexamordsn(Object asigxexamordsn) {
        return findByProperty(ASIGXEXAMORDSN, asigxexamordsn);
    }

    /**
    * Find all Asignatura entities.
    *
    * @return List<Asignatura> all Asignatura instances
    */
    public List<Asignatura> findAll() {
        log.debug("finding all Asignatura instances");

        try {
            String queryString = "from Asignatura";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Asignatura> findByCriteria(String whereCondition) {
        log.debug("finding Asignatura " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Asignatura model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Asignatura> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Asignatura failed", re);
            throw re;
        }
    }

    public List<Asignatura> findPageAsignatura(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Asignatura findPageAsignatura");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Asignatura model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Asignatura model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberAsignatura() {
        log.debug("finding Asignatura count");

        try {
            String queryString = "select count(*) from Asignatura model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
